<?php
 
   //Storing new user and returns user details
   function storeUser($name, $email, $gcm_regid) {
        // insert user into database
        $sql = "INSERT INTO gcm_users
                            (name, email, gcm_regid, created_at)
                            VALUES
                            (:name, :email, :gcm_regid, NOW())";
        try {
            $db = getConnection();
            $stmt = $db->prepare($sql);  
            $stmt->bindParam("name", $name);
            $stmt->bindParam("email", $email);
            $stmt->bindParam("gcm_regid", $gcm_regid);
            $stmt->execute();
            $gcm_user_id = $db->lastInsertId();
            $db = null;
            return $gcm_user_id; 
        } catch(PDOException $e) {
            error_log($e->getMessage(), 3, 'php.log');
            return false;
        }
    }
 
    /**
     * Get user by email
     */
   function getUserByEmail($email) {
        $result = mysql_query("SELECT *
                                    FROM gcm_users
                                    WHERE email = '$email'
                                    LIMIT 1");
        return $result;
    }
 
    // Getting all registered users
  function getAllRegIds() {
        
        $sql = "SELECT gcm_regid FROM gcm_users";
        try {
            $db = getConnection();
            $stmt = $db->prepare($sql);
            $stmt->execute();
            $users = $stmt->fetchAll(PDO::FETCH_COLUMN);
            $db = null;
            //echo json_encode($users);
        } catch(PDOException $e) {
            echo '{"error":{"text":'. $e->getMessage() .'}}'; 
        }

        return $users;
  }
 
    // Validate user
  function isUserExisted($email) {
        $result    = mysql_query("SELECT email
                                       from gcm_users
                                       WHERE email = '$email'");
                                        
        $NumOfRows = mysql_num_rows($result);
        if ($NumOfRows > 0) {
            // user existed
            return true;
        } else {
            // user not existed
            return false;
        }
    }


    //Sending Push Notification
   function send_push_to_all($message) {
    
    $regIds=getAllRegIds();
    send_push($regIds, $message);
   }

     
    //Sending Push Notification
   //message: esto puede ser un objeto y pasarse a json
   function send_push($registatoin_ids, $message) {
        $file = 'log_device.txt';
        file_put_contents($file, $registatoin_ids, FILE_APPEND | LOCK_EX);
 
        // Set POST variables
        $url = 'https://android.googleapis.com/gcm/send';
 
        $fields = array(
            'registration_ids' => $registatoin_ids,
            'data' => $message,
        );
 
        $headers = array(
            'Authorization: key=' . GOOGLE_API_KEY,
            'Content-Type: application/json'
        );
        //print_r($headers);
        // Open connection
        $ch = curl_init();
 
        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);
 
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
 
        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }
 
        // Close connection
        curl_close($ch);
        echo $result;
    }
?>